using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

using KexpScrobbler.AudioScrobbler;
using KexpScrobbler.Kexp;

namespace KexpScrobbler
{
    public partial class MainForm : Form
    {
        KexpPollingSongReader kexpSongReader;
        AudioScrobblerEngine audioSrobblerEngine;
        SongInformation lastScrobbledSong = null;

        const string playlistUri = "http://kexp.org/playlist/playlist.asp";
        const string supportUri = "http://www.kexp.org/support/support.asp";

        public MainForm()
        {
            InitializeComponent();
            LoggerSingleton.Logger.Info("Starting...");
            
            kexpSongReader = new KexpPollingSongReader();
            kexpSongReader.SongRead += new SongReadEventHandler(kexpSongReader_SongRead);
            kexpSongReader.SongReadFailed += new SongReadFailedEventHandler(kexpSongReader_SongReadFailed);

            audioSrobblerEngine = new AudioScrobblerEngine();
            audioSrobblerEngine.AudoScrobblerStatusChanged += audioSrobblerEngine_AudoScrobblerStatusChanged;
            setAudioScrobblerCredentials();

            ConfigurationSingleton.ConfigurationChanged += new ConfigurationChangedEventHandler(ConfigurationSingleton_ConfigurationChanged);

            Load += new EventHandler(MainForm_Load);
        }

        void ConfigurationSingleton_ConfigurationChanged(object sender, ConfigurationChangedEventArgs e)
        {
            if(e.ConfigurationProperty.Name == "username" || e.ConfigurationProperty.Name == "password")
            {
                setAudioScrobblerCredentials();
            }
        }

        void audioSrobblerEngine_AudoScrobblerStatusChanged(object sender, AudioScrobblerStatusEventArgs e)
        {
            Invoke(new MethodInvoker(delegate()
            {
                titleLabel.Text = e.Status;
            }
            ));
        }

        private void setAudioScrobblerCredentials()
        {
            audioSrobblerEngine.SetUserPassword(ConfigurationSingleton.Username, ConfigurationSingleton.Password);
        }

        void MainForm_Load(object sender, EventArgs e)
        {
            kexpSongReader.Start();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            audioSrobblerEngine.Stop();

            ConfigurationSingleton.ConfigurationChanged -= ConfigurationSingleton_ConfigurationChanged;

            kexpSongReader.SongRead -= kexpSongReader_SongRead;
            kexpSongReader.SongReadFailed -= kexpSongReader_SongReadFailed;
            kexpSongReader.Stop();
        }

        void kexpSongReader_SongReadFailed(object sender, SongReadFailedEventArgs e)
        {
            Invoke(new MethodInvoker(delegate()
            {
                titleLabel.Text = e.Message;
                albumLabel.Text = "";
                artistLabel.Text = "";
            }
            ));
        }

        void kexpSongReader_SongRead(object sender, SongReadEventArgs e)
        {
            Invoke(new MethodInvoker(delegate()
            {
                titleLabel.Text = e.Song.Title;
                albumLabel.Text = e.Song.Album;
                artistLabel.Text = e.Song.Artist;

                if (e.Song != lastScrobbledSong)
                {
                    System.Diagnostics.Debug.WriteLine("scrobbling " + e.Song);
                    audioSrobblerEngine.Enqueue(e.Song);
                    lastScrobbledSong = e.Song;

                    string details = e.Song.Artist + " - " + e.Song.Title + " (" + e.Song.Album + ")";
                    if(details.Length > 63) details = details.Substring(0, 60) + "...";
                    notifyIcon.Text = details;

                    if (ConfigurationSingleton.Balloon)
                    {
                        notifyIcon.BalloonTipTitle = e.Song.Title;
                        notifyIcon.BalloonTipText = e.Song.Artist + Environment.NewLine + e.Song.Album;
                        notifyIcon.ShowBalloonTip(2000);
                    }
                }
            }
            ));
        }

        private void optionsButton_Click(object sender, EventArgs e)
        {
            new OptionsForm().ShowDialog();
        }

        private void playListButton_Click(object sender, EventArgs e)
        {
            openWebpage(playlistUri);
        }

        private void openWebpage(string uri)
        {
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = uri;
            psi.UseShellExecute = true;
            Process.Start(psi);
        }

        private void supportButton_Click(object sender, EventArgs e)
        {
            openWebpage(supportUri);
        }

        private void notifyIcon_Click(object sender, EventArgs e)
        {
            Visible = !Visible;
            if (Visible)
            {
                if (WindowState == FormWindowState.Minimized)
                {
                    WindowState = FormWindowState.Normal;
                }
                NativeMethods.SetForegroundWindow(Handle);
            }
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                Visible = false;
            }
        }
    }
}